local love = require 'love'

local setColor = love.graphics.setColor
local drawg = love.graphics.draw
local drawRectangle = love.graphics.rectangle
local polygon = love.graphics.polygon

local width = Width
local height = Height

local hearts = {}
local heartSize, centerX
local heartCount = 18 -- 心形数量
local heartSpeed = 66 -- 心形移动速度 (像素/秒)
local heartTriangles = {} -- 存储一次计算的三角形数据
local unit, scale
local rectX, rectWidth
local colors = {
    {1, 0.8, 0.8, 1},
    {1, 0.7, 0.7, 1},
    {1, 0.6, 0.6, 1},
    {1, 0.5, 0.5, 1},
    {1, 0.4, 0.4, 1},
    {1, 0.3, 0.3, 1}
}

local cover = {
    image = love.graphics.newImage("assets/cover.jpg"),
    scale = 0,
}

-- 初始化心形三角形数据，只计算一次
local function initHeartTriangles(size)
    unit = size / 4
    heartTriangles = {
        { -- 第一个三角形
            x1 = 0, y1 = unit,
            x2 = unit, y2 = 0,
            x3 = unit * 2, y3 = unit
        },
        { -- 第二个三角形
            x1 = unit * 2, y1 = unit,
            x2 = unit * 3, y2 = 0,
            x3 = unit * 4, y3 = unit
        },
        { -- 第三个三角形
            x1 = 0, y1 = unit,
            x2 = unit, y2 = unit * 2.5,
            x3 = unit * 2, y3 = unit
        },
        { -- 第四个三角形
            x1 = unit * 2, y1 = unit,
            x2 = unit * 3, y2 = unit * 2.5,
            x3 = unit * 4, y3 = unit
        },
        { -- 第五个三角形
            x1 = unit, y1 = unit * 2.5,
            x2 = unit * 2, y2 = unit,
            x3 = unit * 2, y3 = unit * 4
        },
        { -- 第六个三角形
            x1 = unit * 3, y1 = unit * 2.5,
            x2 = unit * 2, y2 = unit,
            x3 = unit * 2, y3 = unit * 4
        }
    }
end

local function init()
    heartSize = height / (heartCount * 2)
    centerX = (width - heartSize) / 2
    rectX = centerX - heartSize / 3
    rectWidth = heartSize * 5 / 3

    initHeartTriangles(heartSize) -- 初始化心形三角形数据

    for i = 1, heartCount + 1 do
        local y = (i - 1) * (heartSize + heartSize)
        table.insert(hearts, {x = centerX, y = y, size = heartSize})
    end

    cover.scale = width / cover.image:getWidth()
end

local function update(dt)
    for _, heart in ipairs(hearts) do
        heart.y = heart.y - heartSpeed * dt

        -- 如果心形移出屏幕顶部，将其移到底部重新开始
        if heart.y <= - heartSize then
            heart.y = height + heartSize
        end
    end
end

-- 绘制单个心形 (使用预计算的三角形数据)
local function drawHeart(x, y, size)
    -- 计算缩放因子
    scale = size / (unit * 4)

    -- 绘制第一个三角形
    setColor(colors[5])
    polygon("fill",
        x + heartTriangles[1].x1 * scale, y + heartTriangles[1].y1 * scale,
        x + heartTriangles[1].x2 * scale, y + heartTriangles[1].y2 * scale,
        x + heartTriangles[1].x3 * scale, y + heartTriangles[1].y3 * scale)

    -- 绘制第四个三角形
    polygon("fill",
        x + heartTriangles[4].x1 * scale, y + heartTriangles[4].y1 * scale,
        x + heartTriangles[4].x2 * scale, y + heartTriangles[4].y2 * scale,
        x + heartTriangles[4].x3 * scale, y + heartTriangles[4].y3 * scale)

    -- 绘制第二个三角形
    setColor(colors[6])
    polygon("fill",
        x + heartTriangles[2].x1 * scale, y + heartTriangles[2].y1 * scale,
        x + heartTriangles[2].x2 * scale, y + heartTriangles[2].y2 * scale,
        x + heartTriangles[2].x3 * scale, y + heartTriangles[2].y3 * scale)

    -- 绘制第三个三角形
    setColor(colors[4])
    polygon("fill",
        x + heartTriangles[3].x1 * scale, y + heartTriangles[3].y1 * scale,
        x + heartTriangles[3].x2 * scale, y + heartTriangles[3].y2 * scale,
        x + heartTriangles[3].x3 * scale, y + heartTriangles[3].y3 * scale)

    -- 绘制第六个三角形
    polygon("fill",
        x + heartTriangles[6].x1 * scale, y + heartTriangles[6].y1 * scale,
        x + heartTriangles[6].x2 * scale, y + heartTriangles[6].y2 * scale,
        x + heartTriangles[6].x3 * scale, y + heartTriangles[6].y3 * scale)

    -- 绘制第五个三角形
    setColor(colors[3])
    polygon("fill",
        x + heartTriangles[5].x1 * scale, y + heartTriangles[5].y1 * scale,
        x + heartTriangles[5].x2 * scale, y + heartTriangles[5].y2 * scale,
        x + heartTriangles[5].x3 * scale, y + heartTriangles[5].y3 * scale)
end

local function drawCover()
    setColor(1, 1, 1)
    drawg(cover.image, 0, 0, 0, cover.scale, cover.scale)

    setColor(1, 1, 1, 0.5)
    drawRectangle("fill", rectX, 0, rectWidth, height)
    drawRectangle("fill", hearts[1].x, 0, hearts[1].size, height)
    setColor(colors[3])
    drawRectangle("line", hearts[1].x, 0, hearts[1].size, height)

    for _, heart in ipairs(hearts) do
        drawHeart(heart.x, heart.y, heart.size)
    end
end

return{
    init = init,
    update = update,
    drawCover = drawCover
}